package com.jaya.budan.pay.wx;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.jaya.budan.R;
import com.jaya.budan.activity.My_Wallet_Recharge_Activity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    // private TextView reulttv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wx_pay_result);
        api = WXAPIFactory.createWXAPI(this, My_Wallet_Recharge_Activity.WXAPPID);

        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Toast.makeText(getApplicationContext(),"onReq",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResp(BaseResp resp) {
//        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle("提示");
            //builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//            builder.show();
            Intent intent;
            int code = resp.errCode;
            switch (code) {
                case 0:
                    Toast.makeText(WXPayEntryActivity.this, "微信支付成功", Toast.LENGTH_LONG).show();
                    intent = new Intent(WXPayEntryActivity.this, My_Wallet_Recharge_Activity.class);
                    intent.putExtra("result", 0);
                    startActivity(intent);
                    finish();
                    break;
                case -1:
                    Toast.makeText(WXPayEntryActivity.this, "微信支付失败", Toast.LENGTH_LONG).show();
                    intent = new Intent(WXPayEntryActivity.this, My_Wallet_Recharge_Activity.class);
                    intent.putExtra("result", -1);
                    startActivity(intent);
                    finish();
                    break;
                case -2:
                    Toast.makeText(WXPayEntryActivity.this, "微信支付取消", Toast.LENGTH_LONG).show();
                    intent = new Intent(WXPayEntryActivity.this, My_Wallet_Recharge_Activity.class);
                    intent.putExtra("result", -2);
                    startActivity(intent);
                    finish();
                    break;
                default:
                    break;
            }
        }
    }
}